/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxitemdetexptapi.p                               */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : Item detail export Process                       */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                 2-APR-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


 define temp-table ttSearchCondition no-undo
       field  tt_site_from    like si_site
       field  tt_site_to      like si_site
       field  tt_part_from    like pt_part
       field  tt_part_to      like pt_part.


 define temp-table ttitemdet no-undo

       field pt_part                  like  pt_mstr.pt_part
       field pt_domain                like  pt_mstr.pt_domain
       field pt_desc1                 like  pt_mstr.pt_desc1
       field pt_desc2                 like  pt_mstr.pt_desc2               
       field pt_um                    like  pt_mstr.pt_um
       field pt_prod_line             like  pt_mstr.pt_prod_line 
       field pt_added                 like  pt_mstr.pt_added
       field pt_dsgn_grp              like  pt_mstr.pt_dsgn_grp                  
       field pt_Promo                 like  pt_mstr.pt_promo                  
       field pt_Part_type             like  pt_mstr.pt_part_type
       field pt_status                like  pt_mstr.pt_status
       field pt_draw                  like  pt_mstr.pt_draw                  
       field pt_group                 like  pt_mstr.pt_group
       field pt_rev                   like  pt_mstr.pt_rev                   
       field pt_drwg_loc              like  pt_mstr.pt_drwg_loc                   
       field pt_drwg_size             like  pt_mstr.pt_drwg_size                 
       field pt_break_cat             like  pt_mstr.pt_break_cat
       field pt__chr01                like  pt_mstr.pt__chr01    
       field ptp_part                 like ptp_det.ptp_part
       field ptp_site                 like ptp_det.ptp_site 
       field ptp_ms                   like ptp_det.ptp_ms                
       field ptp_plan_ord             like ptp_det.ptp_plan_ord          
       field ptp_timefnce             like ptp_det.ptp_timefnce          
       field ptp_ord_pol              like ptp_det.ptp_ord_pol           
       field ptp_ord_qty              like ptp_det.ptp_ord_qty           
       field ptp_ord_per              like ptp_det.ptp_ord_per           
       field ptp_sfty_stk             like ptp_det.ptp_sfty_stk          
       field ptp_sfty_tme             like ptp_det.ptp_sfty_tme          
       field ptp_rop                  like ptp_det.ptp_rop               
       field ptp_rev                  like ptp_det.ptp_rev               
       field ptp_iss_pol              like ptp_det.ptp_iss_pol           
       field ptp_buyer                like ptp_det.ptp_buyer             
       field ptp_vend                 like ptp_det.ptp_vend              
       field ptp_po_site              like ptp_det.ptp_po_site           
       field ptp_pm_code              like ptp_det.ptp_pm_code           
       field ptp_gcfg_type            like ptp_det.ptp_gcfg_type         
       field ptp_insp_loc             like ptp_det.ptp_insp_loc          
       field ptp_ins_rqd              like ptp_det.ptp_ins_rqd           
       field ptp_ins_lead             like ptp_det.ptp_ins_lead          
       field ptp_cum_lead             like ptp_det.ptp_cum_lead          
       field ptp_mfg_lead             like ptp_det.ptp_mfg_lead          
       field ptp_pur_lead             like ptp_det.ptp_pur_lead          
       field ptp_atp_enforcement      like ptp_det.ptp_atp_enforcement   
       field ptp_atp_family           like ptp_det.ptp_atp_family                                              
       field ptp_atp_horizon          like ptp_det.ptp_atp_horizon       
       field ptp_run_seq1             like ptp_det.ptp_run_seq1          
       field ptp_run_seq2             like ptp_det.ptp_run_seq2          
       field ptp_Phantom              like ptp_det.ptp_Phantom           
       field ptp_ord_max              like ptp_det.ptp_ord_max           
       field ptp_ord_min              like ptp_det.ptp_ord_min           
       field ptp_ord_mult             like ptp_det.ptp_ord_mult                                            
       field ptp_op_yield             like ptp_det.ptp_op_yield          
       field ptp_yld_pct              like ptp_det.ptp_yld_pct           
       field ptp_run                  like ptp_det.ptp_run               
       field ptp_setup                like ptp_det.ptp_setup             
       field ptp_network              like ptp_det.ptp_network                                                
       field ptp_routing              like ptp_det.ptp_routing           
       field ptp_bom_code             like ptp_det.ptp_bom_code          
       field ptp_replenishment_mthd   like ptp_det.ptp_replenishment_mthd   
       field pti_part                 like pti_det.pti_part        
       field pti_site                 like pti_det.pti_site        
       field pti_abc                  like pti_det.pti_abc         
       field pti_lot_ser              like pti_det.pti_lot_ser     
       field pti_loc                  like pti_det.pti_loc         
       field pti_loc_type             like pti_det.pti_loc_type    
       field pti_auto_lot             like pti_det.pti_auto_lot    
       field pti_avg_int              like pti_det.pti_avg_int     
       field pti_cyc_int              like pti_det.pti_cyc_int     
       field pti_shelflife            like pti_det.pti_shelflife   
       field pti_critical             like pti_det.pti_critical    
       field pti_rctpo_status         like pti_det.pti_rctpo_status
       field pti_rctpo_active         like pti_det.pti_rctpo_active
       field pti_rctwo_status         like pti_det.pti_rctwo_status
       field pti_rctwo_active         like pti_det.pti_rctwo_active
       field pti_memo_type            like pti_det.pti_memo_type 
       field pt_article            like pt_mstr.pt_article
       field pt_lot_grp            like pt_mstr.pt_lot_grp
       field com_comm_code            like com_mstr.com_comm_code
       field api_status               as   character

       index idx1 pt_part
       index idx2 ptp_site
       .
      
   define buffer b_ttitemdet for ttitemdet.
      
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsttitemdet FOR ttitemdet.

   hresult = dataset dsttitemdet:HANDLE.    
                

  
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "pt/"
       m_archpath    = m_archpath + "pt/"
       m_successpath = m_successpath + "pt/"
       m_errorpath   = m_errorpath + "pt/"
       m_workpath    = m_workpath + "pt/".


   empty temp-table ttitemdet.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

   for each ttSearchCondition. 
    
    if ttSearchCondition.tt_site_to = "" then ttSearchCondition.tt_site_to = hi_char.
    if ttSearchCondition.tt_part_to = "" then ttSearchCondition.tt_part_to = hi_char.
    
     for each pt_mstr no-lock where pt_mstr.pt_domain = global_domain
                                and pt_mstr.pt_part >= ttSearchCondition.tt_part_from                       
                                and pt_mstr.pt_part <= ttSearchCondition.tt_part_to   
                                .                     
       
     find first comd_det  where comd_det.comd_domain = global_domain and
         comd_part = pt_part
         no-lock no-error.
        
        create ttitemdet.
        assign   
               ttitemdet.pt_part                  = pt_mstr.pt_part
               ttitemdet.pt_domain                = pt_mstr.pt_domain
               ttitemdet.pt_desc1                 = replace(replace(replace(replace(replace(pt_mstr.pt_desc1,"\r",""),"\n",""),"""",""),"}",""),"\{","")
               ttitemdet.pt_desc2                 = replace(replace(replace(replace(replace(pt_mstr.pt_desc2,"\r",""),"\n",""),"""",""),"}",""),"\{","")               
               ttitemdet.pt_um                    = pt_mstr.pt_um
               ttitemdet.pt_prod_line             = pt_mstr.pt_prod_line 
               ttitemdet.pt_added                 = pt_mstr.pt_added
               ttitemdet.pt_dsgn_grp              = pt_mstr.pt_dsgn_grp                  
               ttitemdet.pt_Promo                 = pt_mstr.pt_promo                  
               ttitemdet.pt_Part_type             = pt_mstr.pt_part_type
               ttitemdet.pt_status                = pt_mstr.pt_status
               ttitemdet.pt_draw                  = pt_mstr.pt_draw                  
               ttitemdet.pt_group                 = pt_mstr.pt_group
               ttitemdet.pt_rev                   = pt_mstr.pt_rev                   
               ttitemdet.pt_drwg_loc              = pt_mstr.pt_drwg_loc                   
               ttitemdet.pt_drwg_size             = pt_mstr.pt_drwg_size                 
               ttitemdet.pt_break_cat             = pt_mstr.pt_break_cat
               ttitemdet.pt__chr01                = pt_mstr.pt__chr01 
               ttitemdet.ptp_part                 = pt_mstr.pt_part
               ttitemdet.ptp_site                 = pt_mstr.pt_site 
               ttitemdet.ptp_ms                   = pt_mstr.pt_ms                
               ttitemdet.ptp_plan_ord             = pt_mstr.pt_plan_ord          
               ttitemdet.ptp_timefnce             = pt_mstr.pt_timefence          
               ttitemdet.ptp_ord_pol              = pt_mstr.pt_ord_pol           
               ttitemdet.ptp_ord_qty              = pt_mstr.pt_ord_qty           
               ttitemdet.ptp_ord_per              = pt_mstr.pt_ord_per           
               ttitemdet.ptp_sfty_stk             = pt_mstr.pt_sfty_stk          
               ttitemdet.ptp_sfty_tme             = pt_mstr.pt_sfty_time          
               ttitemdet.ptp_rop                  = pt_mstr.pt_rop               
               ttitemdet.ptp_rev                  = pt_mstr.pt_rev               
               ttitemdet.ptp_iss_pol              = pt_mstr.pt_iss_pol           
               ttitemdet.ptp_buyer                = pt_mstr.pt_buyer             
               ttitemdet.ptp_vend                 = pt_mstr.pt_vend              
               ttitemdet.ptp_po_site              = pt_mstr.pt_po_site           
               ttitemdet.ptp_pm_code              = pt_mstr.pt_pm_code           
               ttitemdet.ptp_gcfg_type            = pt_mstr.pt_gcfg_type         
               ttitemdet.ptp_insp_loc             = ""         
               ttitemdet.ptp_ins_rqd              = pt_mstr.pt_insp_rqd           
               ttitemdet.ptp_ins_lead             = pt_mstr.pt_insp_lead          
               ttitemdet.ptp_cum_lead             = pt_mstr.pt_cum_lead          
               ttitemdet.ptp_mfg_lead             = pt_mstr.pt_mfg_lead          
               ttitemdet.ptp_pur_lead             = pt_mstr.pt_pur_lead          
               ttitemdet.ptp_atp_enforcement      = pt_mstr.pt_atp_enforcement   
               ttitemdet.ptp_atp_family           = pt_mstr.pt_atp_family                                              
               ttitemdet.ptp_atp_horizon          = pt_mstr.pt_atp_horizon       
               ttitemdet.ptp_run_seq1             = pt_mstr.pt_run_seq1          
               ttitemdet.ptp_run_seq2             = pt_mstr.pt_run_seq2          
               ttitemdet.ptp_Phantom              = pt_mstr.pt_Phantom           
               ttitemdet.ptp_ord_max              = pt_mstr.pt_ord_max           
               ttitemdet.ptp_ord_min              = pt_mstr.pt_ord_min           
               ttitemdet.ptp_ord_mult             = pt_mstr.pt_ord_mult                                            
               ttitemdet.ptp_op_yield             = pt_mstr.pt_op_yield          
               ttitemdet.ptp_yld_pct              = pt_mstr.pt_yield_pct           
               ttitemdet.ptp_run                  = pt_mstr.pt_run               
               ttitemdet.ptp_setup                = pt_mstr.pt_setup             
               ttitemdet.ptp_network              = pt_mstr.pt_network                                                
               ttitemdet.ptp_routing              = pt_mstr.pt_routing           
               ttitemdet.ptp_bom_code             = pt_mstr.pt_bom_code          
               ttitemdet.ptp_replenishment_mthd   = pt_mstr.pt_replenishment_mthd   
               ttitemdet.pti_part                 = pt_mstr.pt_part        
               ttitemdet.pti_site                 = pt_mstr.pt_site        
               ttitemdet.pti_abc                  = pt_mstr.pt_abc         
               ttitemdet.pti_lot_ser              = pt_mstr.pt_lot_ser     
               ttitemdet.pti_loc                  = pt_mstr.pt_loc         
               ttitemdet.pti_loc_type             = pt_mstr.pt_loc_type    
               ttitemdet.pti_auto_lot             = pt_mstr.pt_auto_lot    
               ttitemdet.pti_avg_int              = pt_mstr.pt_avg_int     
               ttitemdet.pti_cyc_int              = pt_mstr.pt_cyc_int     
               ttitemdet.pti_shelflife            = pt_mstr.pt_shelflife   
               ttitemdet.pti_critical             = pt_mstr.pt_critical    
               ttitemdet.pti_rctpo_status         = pt_mstr.pt_rctpo_status
               ttitemdet.pti_rctpo_active         = pt_mstr.pt_rctpo_active
               ttitemdet.pti_rctwo_status         = pt_mstr.pt_rctwo_status
               ttitemdet.pti_rctwo_active         = pt_mstr.pt_rctwo_active
               ttitemdet.api_status         = "SUCCESS"
               ttitemdet.pti_memo_type            = pt_mstr.pt_memo_type
               ttitemdet.pt_article               = pt_mstr.pt_article
               ttitemdet.pt_lot_grp              = pt_mstr.pt_lot_grp
               ttitemdet.com_comm_code            =  comd_det.comd_comm_code when available comd_det
               .



    for each ptp_det no-lock where ptp_det.ptp_domain = global_domain
                               and ptp_det.ptp_site >=   ttSearchCondition.tt_site_from        
                               and ptp_det.ptp_site <=   ttSearchCondition.tt_site_to 
                               and ptp_det.ptp_part = pt_mstr.pt_part 
                               break by ptp_site 
                               .
        if first-of(ptp_det.ptp_site) then                      
        assign ttitemdet.pt_part                  = pt_mstr.pt_part
               ttitemdet.pt_domain                = pt_mstr.pt_domain
               ttitemdet.pt_desc1                 = pt_mstr.pt_desc1
               ttitemdet.pt_desc2                 = pt_mstr.pt_desc2               
               ttitemdet.pt_um                    = pt_mstr.pt_um
               ttitemdet.pt_prod_line             = pt_mstr.pt_prod_line 
               ttitemdet.pt_added                 = pt_mstr.pt_added
               ttitemdet.pt_dsgn_grp              = pt_mstr.pt_dsgn_grp                  
               ttitemdet.pt_Promo                 = pt_mstr.pt_promo                  
               ttitemdet.pt_Part_type             = pt_mstr.pt_part_type
               ttitemdet.pt_status                = pt_mstr.pt_status
               ttitemdet.pt_draw                  = pt_mstr.pt_draw                  
               ttitemdet.pt_group                 = pt_mstr.pt_group
               ttitemdet.pt_rev                   = pt_mstr.pt_rev                   
               ttitemdet.pt_drwg_loc              = pt_mstr.pt_drwg_loc                   
               ttitemdet.pt_drwg_size             = pt_mstr.pt_drwg_size                 
               ttitemdet.pt_break_cat             = pt_mstr.pt_break_cat
               ttitemdet.pt__chr01                = pt_mstr.pt__chr01 
               ttitemdet.ptp_part                 = ptp_det.ptp_part
               ttitemdet.ptp_site                 = ptp_det.ptp_site 
               ttitemdet.ptp_ms                   = ptp_det.ptp_ms                
               ttitemdet.ptp_plan_ord             = ptp_det.ptp_plan_ord          
               ttitemdet.ptp_timefnce             = ptp_det.ptp_timefnce          
               ttitemdet.ptp_ord_pol              = ptp_det.ptp_ord_pol           
               ttitemdet.ptp_ord_qty              = ptp_det.ptp_ord_qty           
               ttitemdet.ptp_ord_per              = ptp_det.ptp_ord_per           
               ttitemdet.ptp_sfty_stk             = ptp_det.ptp_sfty_stk          
               ttitemdet.ptp_sfty_tme             = ptp_det.ptp_sfty_tme          
               ttitemdet.ptp_rop                  = ptp_det.ptp_rop               
               ttitemdet.ptp_rev                  = ptp_det.ptp_rev               
               ttitemdet.ptp_iss_pol              = ptp_det.ptp_iss_pol           
               ttitemdet.ptp_buyer                = ptp_det.ptp_buyer             
               ttitemdet.ptp_vend                 = ptp_det.ptp_vend              
               ttitemdet.ptp_po_site              = ptp_det.ptp_po_site           
               ttitemdet.ptp_pm_code              = ptp_det.ptp_pm_code           
               ttitemdet.ptp_gcfg_type            = ptp_det.ptp_gcfg_type         
               ttitemdet.ptp_insp_loc             = ptp_det.ptp_insp_loc          
               ttitemdet.ptp_ins_rqd              = ptp_det.ptp_ins_rqd           
               ttitemdet.ptp_ins_lead             = ptp_det.ptp_ins_lead          
               ttitemdet.ptp_cum_lead             = ptp_det.ptp_cum_lead          
               ttitemdet.ptp_mfg_lead             = ptp_det.ptp_mfg_lead          
               ttitemdet.ptp_pur_lead             = ptp_det.ptp_pur_lead          
               ttitemdet.ptp_atp_enforcement      = ptp_det.ptp_atp_enforcement   
               ttitemdet.ptp_atp_family           = ptp_det.ptp_atp_family                                              
               ttitemdet.ptp_atp_horizon          = ptp_det.ptp_atp_horizon       
               ttitemdet.ptp_run_seq1             = ptp_det.ptp_run_seq1          
               ttitemdet.ptp_run_seq2             = ptp_det.ptp_run_seq2          
               ttitemdet.ptp_Phantom              = ptp_det.ptp_Phantom           
               ttitemdet.ptp_ord_max              = ptp_det.ptp_ord_max           
               ttitemdet.ptp_ord_min              = ptp_det.ptp_ord_min           
               ttitemdet.ptp_ord_mult             = ptp_det.ptp_ord_mult                                            
               ttitemdet.ptp_op_yield             = ptp_det.ptp_op_yield          
               ttitemdet.ptp_yld_pct              = ptp_det.ptp_yld_pct           
               ttitemdet.ptp_run                  = ptp_det.ptp_run               
               ttitemdet.ptp_setup                = ptp_det.ptp_setup             
               ttitemdet.ptp_network              = ptp_det.ptp_network                                                
               ttitemdet.ptp_routing              = ptp_det.ptp_routing           
               ttitemdet.ptp_bom_code             = ptp_det.ptp_bom_code          
               ttitemdet.ptp_replenishment_mthd   = ptp_det.ptp_replenishment_mthd   .

       else do:
              create ttitemdet.
              assign ttitemdet.pt_part                  = pt_mstr.pt_part
               ttitemdet.pt_domain                = pt_mstr.pt_domain
               ttitemdet.pt_desc1                 = pt_mstr.pt_desc1
               ttitemdet.pt_desc2                 = pt_mstr.pt_desc2               
               ttitemdet.pt_um                    = pt_mstr.pt_um
               ttitemdet.pt_prod_line             = pt_mstr.pt_prod_line 
               ttitemdet.pt_added                 = pt_mstr.pt_added
               ttitemdet.pt_dsgn_grp              = pt_mstr.pt_dsgn_grp                  
               ttitemdet.pt_Promo                 = pt_mstr.pt_promo                  
               ttitemdet.pt_Part_type             = pt_mstr.pt_part_type
               ttitemdet.pt_status                = pt_mstr.pt_status
               ttitemdet.pt_draw                  = pt_mstr.pt_draw                  
               ttitemdet.pt_group                 = pt_mstr.pt_group
               ttitemdet.pt_rev                   = pt_mstr.pt_rev                   
               ttitemdet.pt_drwg_loc              = pt_mstr.pt_drwg_loc                   
               ttitemdet.pt_drwg_size             = pt_mstr.pt_drwg_size                 
               ttitemdet.pt_break_cat             = pt_mstr.pt_break_cat
               ttitemdet.pt__chr01                = pt_mstr.pt__chr01 
               ttitemdet.ptp_part                 = ptp_det.ptp_part
               ttitemdet.ptp_site                 = ptp_det.ptp_site 
               ttitemdet.ptp_ms                   = ptp_det.ptp_ms                
               ttitemdet.ptp_plan_ord             = ptp_det.ptp_plan_ord          
               ttitemdet.ptp_timefnce             = ptp_det.ptp_timefnce          
               ttitemdet.ptp_ord_pol              = ptp_det.ptp_ord_pol           
               ttitemdet.ptp_ord_qty              = ptp_det.ptp_ord_qty           
               ttitemdet.ptp_ord_per              = ptp_det.ptp_ord_per           
               ttitemdet.ptp_sfty_stk             = ptp_det.ptp_sfty_stk          
               ttitemdet.ptp_sfty_tme             = ptp_det.ptp_sfty_tme          
               ttitemdet.ptp_rop                  = ptp_det.ptp_rop               
               ttitemdet.ptp_rev                  = ptp_det.ptp_rev               
               ttitemdet.ptp_iss_pol              = ptp_det.ptp_iss_pol           
               ttitemdet.ptp_buyer                = ptp_det.ptp_buyer             
               ttitemdet.ptp_vend                 = ptp_det.ptp_vend              
               ttitemdet.ptp_po_site              = ptp_det.ptp_po_site           
               ttitemdet.ptp_pm_code              = ptp_det.ptp_pm_code           
               ttitemdet.ptp_gcfg_type            = ptp_det.ptp_gcfg_type         
               ttitemdet.ptp_insp_loc             = ptp_det.ptp_insp_loc          
               ttitemdet.ptp_ins_rqd              = ptp_det.ptp_ins_rqd           
               ttitemdet.ptp_ins_lead             = ptp_det.ptp_ins_lead          
               ttitemdet.ptp_cum_lead             = ptp_det.ptp_cum_lead          
               ttitemdet.ptp_mfg_lead             = ptp_det.ptp_mfg_lead          
               ttitemdet.ptp_pur_lead             = ptp_det.ptp_pur_lead          
               ttitemdet.ptp_atp_enforcement      = ptp_det.ptp_atp_enforcement   
               ttitemdet.ptp_atp_family           = ptp_det.ptp_atp_family                                              
               ttitemdet.ptp_atp_horizon          = ptp_det.ptp_atp_horizon       
               ttitemdet.ptp_run_seq1             = ptp_det.ptp_run_seq1          
               ttitemdet.ptp_run_seq2             = ptp_det.ptp_run_seq2          
               ttitemdet.ptp_Phantom              = ptp_det.ptp_Phantom           
               ttitemdet.ptp_ord_max              = ptp_det.ptp_ord_max           
               ttitemdet.ptp_ord_min              = ptp_det.ptp_ord_min           
               ttitemdet.ptp_ord_mult             = ptp_det.ptp_ord_mult                                            
               ttitemdet.ptp_op_yield             = ptp_det.ptp_op_yield          
               ttitemdet.ptp_yld_pct              = ptp_det.ptp_yld_pct           
               ttitemdet.ptp_run                  = ptp_det.ptp_run               
               ttitemdet.ptp_setup                = ptp_det.ptp_setup             
               ttitemdet.ptp_network              = ptp_det.ptp_network                                                
               ttitemdet.ptp_routing              = ptp_det.ptp_routing           
               ttitemdet.ptp_bom_code             = ptp_det.ptp_bom_code          
               ttitemdet.ptp_replenishment_mthd   = ptp_det.ptp_replenishment_mthd   .

       end. /* else do */

       end. /* for each ptp_det */

       for each pti_det where pti_det.oid_pt_mstr = pt_mstr.oid_pt_mstr no-lock.

       find si_mstr where si_domain =  global_domain
                      and si_mstr.oid_si_mstr = pti_det.oid_si_mstr
                      no-lock no-error.

       find first b_ttitemdet where b_ttitemdet.ptp_site = si_mstr.si_site
                                and b_ttitemdet.pt_part = pt_mstr.pt_part  no-error.
       if available b_ttitemdet then 
       do:
              assign                         
               b_ttitemdet.pti_part                 = pti_det.pti_part        
               b_ttitemdet.pti_site                 = si_mstr.si_site        
               b_ttitemdet.pti_abc                  = pti_det.pti_abc         
               b_ttitemdet.pti_lot_ser              = pti_det.pti_lot_ser     
               b_ttitemdet.pti_loc                  = pti_det.pti_loc         
               b_ttitemdet.pti_loc_type             = pti_det.pti_loc_type    
               b_ttitemdet.pti_auto_lot             = pti_det.pti_auto_lot    
               b_ttitemdet.pti_avg_int              = pti_det.pti_avg_int     
               b_ttitemdet.pti_cyc_int              = pti_det.pti_cyc_int     
               b_ttitemdet.pti_shelflife            = pti_det.pti_shelflife   
               b_ttitemdet.pti_critical             = pti_det.pti_critical    
               b_ttitemdet.pti_rctpo_status         = pti_det.pti_rctpo_status
               b_ttitemdet.pti_rctpo_active         = pti_det.pti_rctpo_active
               b_ttitemdet.pti_rctwo_status         = pti_det.pti_rctwo_status
               b_ttitemdet.pti_rctwo_active         = pti_det.pti_rctwo_active
               b_ttitemdet.pti_memo_type            = pti_det.pti_memo_type       
                    .
               
       end. /* if available b_ttitemdet */

       else
       do:
              create ttitemdet.
              assign ttitemdet.pt_part                  = pt_mstr.pt_part
               ttitemdet.pt_domain                = pt_mstr.pt_domain
               ttitemdet.pt_desc1                 = pt_mstr.pt_desc1
               ttitemdet.pt_desc2                 = pt_mstr.pt_desc2               
               ttitemdet.pt_um                    = pt_mstr.pt_um
               ttitemdet.pt_prod_line             = pt_mstr.pt_prod_line 
               ttitemdet.pt_added                 = pt_mstr.pt_added
               ttitemdet.pt_dsgn_grp              = pt_mstr.pt_dsgn_grp                  
               ttitemdet.pt_Promo                 = pt_mstr.pt_promo                  
               ttitemdet.pt_Part_type             = pt_mstr.pt_part_type
               ttitemdet.pt_status                = pt_mstr.pt_status
               ttitemdet.pt_draw                  = pt_mstr.pt_draw                  
               ttitemdet.pt_group                 = pt_mstr.pt_group
               ttitemdet.pt_rev                   = pt_mstr.pt_rev                   
               ttitemdet.pt_drwg_loc              = pt_mstr.pt_drwg_loc                   
               ttitemdet.pt_drwg_size             = pt_mstr.pt_drwg_size                 
               ttitemdet.pt_break_cat             = pt_mstr.pt_break_cat
               ttitemdet.pt__chr01                = pt_mstr.pt__chr01 
               ttitemdet.ptp_part                 = pt_mstr.pt_part
               ttitemdet.ptp_site                 = pt_mstr.pt_site 
               ttitemdet.ptp_ms                   = pt_mstr.pt_ms                
               ttitemdet.ptp_plan_ord             = pt_mstr.pt_plan_ord          
               ttitemdet.ptp_timefnce             = pt_mstr.pt_timefence          
               ttitemdet.ptp_ord_pol              = pt_mstr.pt_ord_pol           
               ttitemdet.ptp_ord_qty              = pt_mstr.pt_ord_qty           
               ttitemdet.ptp_ord_per              = pt_mstr.pt_ord_per           
               ttitemdet.ptp_sfty_stk             = pt_mstr.pt_sfty_stk          
               ttitemdet.ptp_sfty_tme             = pt_mstr.pt_sfty_time          
               ttitemdet.ptp_rop                  = pt_mstr.pt_rop               
               ttitemdet.ptp_rev                  = pt_mstr.pt_rev               
               ttitemdet.ptp_iss_pol              = pt_mstr.pt_iss_pol           
               ttitemdet.ptp_buyer                = pt_mstr.pt_buyer             
               ttitemdet.ptp_vend                 = pt_mstr.pt_vend              
               ttitemdet.ptp_po_site              = pt_mstr.pt_po_site           
               ttitemdet.ptp_pm_code              = pt_mstr.pt_pm_code           
               ttitemdet.ptp_gcfg_type            = pt_mstr.pt_gcfg_type         
               ttitemdet.ptp_insp_loc             = ""          
               ttitemdet.ptp_ins_rqd              = pt_mstr.pt_insp_rqd           
               ttitemdet.ptp_ins_lead             = pt_mstr.pt_insp_lead          
               ttitemdet.ptp_cum_lead             = pt_mstr.pt_cum_lead          
               ttitemdet.ptp_mfg_lead             = pt_mstr.pt_mfg_lead          
               ttitemdet.ptp_pur_lead             = pt_mstr.pt_pur_lead          
               ttitemdet.ptp_atp_enforcement      = pt_mstr.pt_atp_enforcement   
               ttitemdet.ptp_atp_family           = pt_mstr.pt_atp_family                                              
               ttitemdet.ptp_atp_horizon          = pt_mstr.pt_atp_horizon       
               ttitemdet.ptp_run_seq1             = pt_mstr.pt_run_seq1          
               ttitemdet.ptp_run_seq2             = pt_mstr.pt_run_seq2          
               ttitemdet.ptp_Phantom              = pt_mstr.pt_Phantom           
               ttitemdet.ptp_ord_max              = pt_mstr.pt_ord_max           
               ttitemdet.ptp_ord_min              = pt_mstr.pt_ord_min           
               ttitemdet.ptp_ord_mult             = pt_mstr.pt_ord_mult                                            
               ttitemdet.ptp_op_yield             = pt_mstr.pt_op_yield          
               ttitemdet.ptp_yld_pct              = pt_mstr.pt_yield_pct           
               ttitemdet.ptp_run                  = pt_mstr.pt_run               
               ttitemdet.ptp_setup                = pt_mstr.pt_setup             
               ttitemdet.ptp_network              = pt_mstr.pt_network                                                
               ttitemdet.ptp_routing              = pt_mstr.pt_routing           
               ttitemdet.ptp_bom_code             = pt_mstr.pt_bom_code          
               ttitemdet.ptp_replenishment_mthd   = pt_mstr.pt_replenishment_mthd 
               ttitemdet.pti_part                 = pti_det.pti_part        
               ttitemdet.pti_site                 = si_mstr.si_site        
               ttitemdet.pti_abc                  = pti_det.pti_abc         
               ttitemdet.pti_lot_ser              = pti_det.pti_lot_ser     
               ttitemdet.pti_loc                  = pti_det.pti_loc         
               ttitemdet.pti_loc_type             = pti_det.pti_loc_type    
               ttitemdet.pti_auto_lot             = pti_det.pti_auto_lot    
               ttitemdet.pti_avg_int              = pti_det.pti_avg_int     
               ttitemdet.pti_cyc_int              = pti_det.pti_cyc_int     
               ttitemdet.pti_shelflife            = pti_det.pti_shelflife   
               ttitemdet.pti_critical             = pti_det.pti_critical    
               ttitemdet.pti_rctpo_status         = pti_det.pti_rctpo_status
               ttitemdet.pti_rctpo_active         = pti_det.pti_rctpo_active
               ttitemdet.pti_rctwo_status         = pti_det.pti_rctwo_status
               ttitemdet.pti_rctwo_active         = pti_det.pti_rctwo_active
               ttitemdet.pti_memo_type            = pti_det.pti_memo_type.

       end. /* for each pti_det where pti_det.oid_pt_mstr = pt_mstr.oid_pt_mstr no-lock. */   

     end. /*    for each pti_det where  */
     
          
     end. /* for each pt_mstr */
   end . /* for each ttSearchCondition.  */

  end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
